"""
============================
# @Time    : 2024/6/20 14:32
# @Author  : Zhang Wenjin
# @FileName: validate_request.py
===========================
"""
from functools import wraps

from utils.base_response import BaseResponse
from utils.base_response_code import BaseResponseCode


def validate_request(serializer_class):
    def decorator(view_func):
        @wraps(view_func)
        def wrapped_view(request, *args, **kwargs):
            if request.method == 'GET':
                serializer = serializer_class(data=request.GET)
            else:
                serializer = serializer_class(data=request.data)
            if serializer.is_valid():
                return view_func(request, serializer=serializer, *args, **kwargs)
            else:
                return BaseResponse(BaseResponseCode.INVALID_PARAMETER, "invalid parameters", None)

        return wrapped_view

    return decorator
